package net.homelinux.chaoswg.io.renga.core.gui;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

import net.homelinux.chaoswg.io.renga.core.gui.operations.Control;
import net.homelinux.chaoswg.io.renga.core.gui.operations.Operation;

//This class is farely simple too
public class StatusBar extends JPanel implements Observer{
    final Control control;
    //We need 4 labels to show what the four operations are doing
    //in case of a move, click, shift-move and double-click
    final JLabel none;
    final JLabel shift;
    final JLabel click;
    final JLabel doubleClick;
    
    public StatusBar (final Control toControl) {
        //We first safe the control to observer
        this.control = toControl;
        //And add ourself as observer
        this.control.addObserver(this);
        
        //Now we arrange the labels
        this.setLayout(new  GridLayout (1,0));

        //Every pair on it's own panel (makes it looking nice)
        //a little boilerplated but hey it's java there are at the very least 2000 LoC to much
        //in this project
        final JPanel nonePanel = new JPanel (new FlowLayout (FlowLayout.LEADING));
        final JPanel shiftPanel = new JPanel (new FlowLayout (FlowLayout.LEADING));
        final JPanel clickPanel = new JPanel (new FlowLayout (FlowLayout.LEADING));
        final JPanel doublePanel = new JPanel (new FlowLayout (FlowLayout.LEADING));

        //To give them statusbar optic
        //we lower them
        nonePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        shiftPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        clickPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
        doublePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));

        //And add a pair of each to the panels
        nonePanel.add(new JLabel ("Drag:"));
        nonePanel.add(none = new JLabel  (""));

        shiftPanel.add(new JLabel ("Shift-Drag:"));
        shiftPanel.add(shift = new JLabel (""));
        
        clickPanel.add (new JLabel ("Click:"));
        clickPanel.add (click = new JLabel (""));
        
        doublePanel.add(new JLabel ("Double-Click:"));
        doublePanel.add(doubleClick = new JLabel(""));
        
        //and add the panels
        this.add (nonePanel);
        this.add (shiftPanel);
        this.add (clickPanel);
        this.add (doublePanel);
    }
    
    //If something happens
    public void update (final Observable observable, final Object obj) {
        //if the control we observe chnages
        if (observable == this.control) {
            // We find out which operation is selected
            final Operation operation = control.getSelected();
            if (operation != null) {
                //if there is one 
                //we ask it for the operations to tell us what 
                //happens on the four operations specified above 
                none.setText(operation.getMoveActionName());
                shift.setText(operation.getShiftMoveActionName());
                click.setText(operation.getClickActionName());
                doubleClick.setText(operation.getDoubleClickActionName());
            }
            else {
                //if nothing is selected we can't show something
                none.setText("");
                shift.setText("");
                click.setText("");
                doubleClick.setText("");
            }
        }
    }
}
